package com.yj.cloudnote.controller;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;
import com.yj.cloudnote.service.DirService;
import org.bson.BSON;
import org.bson.BasicBSONObject;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: jy
 * @Description:
 * @Date: 10:19 2018/4/26
 */
@Controller
@RequestMapping(value = "/dir")
public class DirController {
    @Autowired
    private DirService dirService;

    @RequestMapping(value = "/getDirs")
    @ResponseBody
    public Map<String,Object> getDirs(@RequestBody Map<String,String> params) {
        String username = params.get("username");
        Map<String,Object> res = new HashMap<>();
        if(username != null) {
            res.put("dirs",dirService.getDirByUName(username));
            res.put("dirCount",dirService.getDirCount(username));
            res.put("fileCount",dirService.getFileCount(username));
        }
        return res;
    }

    @RequestMapping(value = "/updateDirs")
    @ResponseBody
    public Map<String,Object> updateDirs(@RequestBody Map<String,Object> params) {
        String username = (String) params.get("username");
        int dirCount = (int)params.get("dirCount");
        int fileCount = (int)params.get("fileCount");
        ArrayList dirs = (ArrayList) params.get("dirs");
        DBObject dirsDBObject = (DBObject) JSON.parse(JSONObject.toJSONString(dirs));
        Map<String,Object> map = new HashMap<>();
        map.put("username",username);
        map.put("dirCount",dirCount);
        map.put("fileCount",fileCount);
        map.put("dirs",dirsDBObject);
        dirService.updateDir(map);
        return null;
    }
}
